require('find')
require('time')
require('date')

p /^(ABC|DEF)*$/ =~ 'ABCDEF'
p /(.)(?:\d\d)+(.)/ =~ '123456'
p /(?:BR-[FPIQDSTU]-(?:00[1-9]|0[1-9][0-9]|[1-9][0-9][0-9])$)|(?:PR-[FPIQDSTU](?:-(00[1-9]|0[1-9][0-9]|[1-9][0-9][0-9])){2}$)|(?:TC-[FPIQDSTU](?:-(00[1-9]|0[1-9][0-9]|[1-9][0-9][0-9])){3}$)|(?:WBS-[FPIQDSTU](?:-(00[1-9]|0[1-9][0-9]|[1-9][0-9][0-9])){3}$)/ =~ "BR-F-001"
p /(?:BR-[FPIQDSTU]-(?:00[1-9]|0[1-9][0-9]|[1-9][0-9][0-9])$)|(?:PR-[FPIQDSTU](?:-(00[1-9]|0[1-9][0-9]|[1-9][0-9][0-9])){2}$)|(?:TC-[FPIQDSTU](?:-(00[1-9]|0[1-9][0-9]|[1-9][0-9][0-9])){3}$)|(?:WBS-[FPIQDSTU](?:-(00[1-9]|0[1-9][0-9]|[1-9][0-9][0-9])){3}$)/ =~ "PR-F-001"
p /(?:BR-[FPIQDSTU]-(?:00[1-9]|0[1-9][0-9]|[1-9][0-9][0-9])$)|(?:PR-[FPIQDSTU](?:-(00[1-9]|0[1-9][0-9]|[1-9][0-9][0-9])){2}$)|(?:TC-[FPIQDSTU](?:-(00[1-9]|0[1-9][0-9]|[1-9][0-9][0-9])){3}$)|(?:WBS-[FPIQDSTU](?:-(00[1-9]|0[1-9][0-9]|[1-9][0-9][0-9])){3}$)/ =~ "TC-F-001-001"
p /(?:BR-[FPIQDSTU]-(?:00[1-9]|0[1-9][0-9]|[1-9][0-9][0-9])$)|(?:PR-[FPIQDSTU](?:-(00[1-9]|0[1-9][0-9]|[1-9][0-9][0-9])){2}$)|(?:TC-[FPIQDSTU](?:-(00[1-9]|0[1-9][0-9]|[1-9][0-9][0-9])){3}$)|(?:WBS-[FPIQDSTU](?:-(00[1-9]|0[1-9][0-9]|[1-9][0-9][0-9])){3}$)/ =~ "WBS-F-001-001-01"
p $1
p $2
p $3
p $&
'123456'.scan(/(\d\d)+?/) do |matched|
  p matched
end
origin = '正则表达式真难啊，怎么这么难懂！'
p origin.gsub(/真难/, '真简单').gsub(/难懂/, '易懂')
=begin
in_str = $stdin.gets()
p in_str
=end
text = 'hello world!\n'
File.write('.\\file\\hello.txt', text)
p File.read('.\\file\\hello.txt')
File.write('.\\file\\hello.txt', 'Ruby on Rails', 5)
p File.read('.\\file\\hello.txt')

=begin
$stdin.each_line do |line|
  printf("%3d %s", $stdin.lineno, line)
end
=end

def traverse(path)
  if File.directory?(path)
    dir = Dir.open(path)
    while name = dir.read
      next if name == '.'
      next if name == '..'
      traverse(path + '/' + name)
    end
  else
    process_file(path)
  end
end

def process_file(path)
  puts path
end

traverse('./file')

IGNORES = [/^\./, /^\.git$/, /^\.vscode$/, /^\.ruby-lsp$/]

def listdir(top)
  Find.find(top) do |path|
    if FileTest.directory?(path)
      dir, base = File.split(path)
      IGNORES.each do |re|
        if re =~ base
          Find.prune
        end
      end
      puts path
    end
  end
end

puts '-' * 30
listdir('../MyTestRuby')

def print_libraries

end

p Encoding.compatible?('AB'.encode('GBK'), '你'.encode('GBK'))
p Encoding.compatible?('AB'.encode('GBK'), '你'.encode('UTF-8'))
p Encoding.compatible?('AB'.encode('UTF-8'), '你'.encode('GBK'))
p Encoding.compatible?('AB'.encode('UTF-8'), '你'.encode('UTF-8'))
p Encoding.compatible?('你'.encode('UTF-8'), '你'.encode('GBK'))
p Encoding.default_external
p Encoding.default_internal

t = Time.now
p t.to_s
p t.strftime('%Y-%m-%d %H:%M:%S')
p Time.strptime('9/1/2024', '%m/%d/%Y').to_s

p Date.today

def age_of_ruby
  day = Date.new(1993, 2, 24)
  now = Date.today
  diff = now - day
  puts 'Ruby is ' + (diff.to_i / 365).to_s + ' years old!'
end

age_of_ruby
